package org.example.service;

import cn.hutool.core.util.RandomUtil;
import jakarta.annotation.Resource;
import org.example.common.MD5Util;
import org.example.common.Result;
import org.example.dao.UserDao;
import org.example.entity.EMData;
import org.example.entity.User;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service

public class ShopService {

    @Resource
    private UserDao userDao;

    @Resource
    private Result result;

    @Resource
    private EmailService emailService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public Result login(User user) {
        // 非空判断
        if(Objects.equals(user.getAccount(), "") || Objects.equals(user.getPassword(), "")) {
            result.setCode("-1");
            result.setMsg("账号密码不能为空!");
            return result;
        }

        User userInfo = userDao.selectUserByAccount(user.getAccount());
        if(userInfo == null) {
            result.setCode("-1");
            result.setMsg("用户不存在");
            return result;
        } else {
            String pwd = userDao.getPwdByAccount(userInfo.getAccount());
            String password = MD5Util.md5(user.getPassword());
            if(!Objects.equals(pwd, password)) {
                result.setCode("-1");
                result.setMsg("账号密码错误");
                return result;
            } else {
                result.setCode("0");
                result.setMsg("登录成功");
                result.setData(user);
                return result;
            }
        }
    }

    public User addUser(User userInfo) {


        // 生成用戶account
        String account = "user_" + System.currentTimeMillis()/1000;

        userInfo.setAccount(account);

        userDao.addUser(userInfo);

        return userInfo;
    }

    public Result checkUsername(String username) {
        if(Objects.equals(username, "") || username == null) {
            result.setCode("-1");
            result.setMsg("用户名不能为空");
            return result;
        }

        User info = userDao.selectUserByUsername(username);
        if(info != null) {
            result.setCode("-1");
            result.setMsg("用户名已存在");
            return result;
        } else {
            result.setCode("0");
            result.setMsg("用户名可用");
            return result;
        }
    }

    // 检查邮箱是否可用
    public Result checkEmail(String email) {
        User user = userDao.selectUserByEmail(email);
        if(user != null) {
            result.setCode("-1");
            result.setMsg("该邮箱已绑定其他用户");
        } else {
            result.setCode("0");
            result.setMsg("邮箱可用");
        }
        return result;
    }

    public Result checkMobile(String mobile) {
        User user = userDao.selectUserByMobile(mobile);
        if(user != null) {
            result.setCode("-1");
            result.setMsg("该手机号码已绑定其他用户");
        } else {
            result.setCode("0");
            result.setMsg("手机号码可用");
        }
        return result;
    }

    //检查邮箱是否注册过账号
    public Result useEmail(String email) {
        User user =  userDao.useEmail(email);
        if(user != null) {
            // 存在
            result.setCode("0");
            result.setMsg(user.getAccount());
            return result;
        } else {
            result.setCode("-1");
            result.setMsg(null);
            return result;
        }
    }

    // 检查手机号是否注册过账号
    public Result useMobile(String mobile) {
        User user =  userDao.useMobile(mobile);
        if(user != null) {
            // 存在
            result.setCode("0");
            result.setMsg(user.getAccount());
            return result;
        } else {
            result.setCode("-1");
            result.setMsg(null);
            return result;
        }
    }

    // 获取邮箱验证码
    public Result sendEmailCode(User user) {
        String email = user.getEmail();
        //生成6位验证码
        String emailCode = RandomUtil.randomNumbers(6);
        result.setCode("0");
        result.setMsg(emailCode);
        //2.将验证码保存到redis中 5分钟
        stringRedisTemplate.opsForValue().set(email,emailCode,5, TimeUnit.MINUTES);
        //3.发送携带验证码的邮件到指定邮箱
        emailService.sendSimpleMail(email,"邮箱验证码",emailCode);


        return result;
    }

    // 修改密码
    public Result editPassword(User user) {
        String password = MD5Util.md5(user.getPassword());
        userDao.editPassword(user.getAccount(),password);
        result.setCode("0");
        result.setMsg("密码修改成功!");
        return result;
    }

    // 检查邮箱验证码
    public Result checkEmailCode(EMData emData) {
        String redisCode = stringRedisTemplate.opsForValue().get(emData.getEmail());
        if(Objects.equals(redisCode,emData.getCode())) {
            result.setCode("0");
        } else {
            result.setCode("-1");
        }
        return result;
    }

    // 检查手机验证码
    public Result checkMobileCode(EMData emData) {
        String redisCode = stringRedisTemplate.opsForValue().get(emData.getEmail());
        if(Objects.equals(redisCode,emData.getCode())) {
            result.setCode("0");
        } else {
            result.setCode("-1");
        }
        return result;
    }
}
